<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell>
      <template #title>
        <div class="title-box">
          <span>{{ obj.title }}</span>
          <img class="thumb" :src="obj.cover.images[0]" v-if="obj.cover.type === 1" />
        </div>
        <!-- 三张图片 -->
        <div class="thumb-box" v-if="obj.cover.type > 1">
          <img class="thumb" v-for="(imgUrl, index) in obj.cover.images" :key="index" :src="imgUrl" />
        </div>
      </template>
      <template #label>
        <div class="label-box">
          <div>
            <span>{{obj.aut_name}}</span>
            <span>{{obj.comm_count}}评论</span>
            <span>{{obj.pubdate}}</span>
          </div>
          <van-icon name="cross" @click="show=true" />
        </div>
      </template>
    </van-cell>
    <van-action-sheet
      v-model="show"
      :actions="actions"
      @select="onSelect"
      get-container="body"
      :cancel-text="cancelText"
      @cancel="onCancel"
    />
  </div>
</template>
<script>
import { articleDislikeAPI, articleReportsAPI } from '@/api'
import { Notify } from 'vant'
import { firstActions, secondActions } from '@/api/reports'
export default {
  props: {
    obj: {
      type: Object
    },
    default() {
      return {}
    },
    methods: {}
  },
  data() {
    return {
      show: false,
      actions: firstActions,
      cancelText: '取消'
    }
  },
  methods: {
    async onSelect(action) {
      console.log(action)
      // action绑定的数据对象, index是索引
      if (action.name === '反馈垃圾内容') {
        console.log(action.name)
        // 证明点击的是反馈垃圾内容
        this.actions = secondActions
        console.log(this.actions)
        this.cancelText = '返回' // 修改底部按钮显示文字
      } else if (action.name === '不感兴趣') {
        try {
          await articleDislikeAPI({ target: this.obj.art_id })
          Notify({ type: 'success', message: '反馈成功' })
          this.show = false
        } catch (err) {
          Notify({ type: 'warning', message: '反馈失败-联系程序员' })
          this.show = false
        }
      } else {
        try {
          await articleReportsAPI({
            target: this.obj.art_id,
            type: action.value,
            remark: '问题'
          })
          Notify({ type: 'success', message: '反馈成功' })
          this.show = false
        } catch (err) {
          Notify({ type: 'warning', message: '反馈失败-联系程序员' })
          this.show = false
        }
      }
    },
    onCancel() {
      if (this.cancelText === '返回') {
        this.actions = firstActions
        this.cancelText = '取消'
        this.show = true // 点返回不要让弹窗关闭, 点取消文字按钮, 弹窗默认关闭
      }
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
